package com.shop.common.comstant;

import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.*;

/**
 * @author 'banxia'
 * @version 1.0
 * @description 用户模块储存的常量
 * @date 2023/2/21 16:51
 */
@Slf4j
public class Constant {

    private static HashMap<String,String> keyValue=new HashMap<>();

    //加载白名单
    static{
        //加载白名单
        try (
                InputStream resourceAsStream = Constant.class.getResourceAsStream("/keyValue.properties");
        ) {
            Properties properties = new Properties();
            properties.load(resourceAsStream);
            Set<String> strings = properties.stringPropertyNames();
            for (String key : strings) {
                String value= properties.getProperty(key);
                keyValue.put(key,value);
            }
        } catch (Exception e) {
            log.error("加载‘键值对的映射关系’出错:{}",e.getMessage());
            e.printStackTrace();
        }
    }

    //用户
    //key
    public static final String UserLoginType="userLogin";
    public static final String UserRegisterType="userRegister";
    public static final String UserUploadFileType="userUploadFile";
    //value
    private static final String UserRegisterCodePre="user:register:code:";
    private static final String UserLoginCodePre="user:login:code:";
    private static final String UserUploadFile="user/"; //比如头像或其他用途由用户来补充

    //商家
    //key
    public static final String MerchantLoginType="merchantLogin";
    public static final String MerchantRegisterType="merchantRegister";
    public static final String MerchantUploadFileType="merchantUploadFile";
    //value
    private static final String MerchantRegisterCodePre="merchant:register:code:";
    private static final String MerchantLoginCodePre="merchant:login:code:";
    private static final String MerchantUploadFile="merchant/"; //比如头像或其他用途由用户来补充

//    private static HashMap<String,String> keyValue =new HashMap<>();

    static {
        //用户登录验证码
        keyValue.put(UserLoginType,UserLoginCodePre);
        //用户注册验证码
        keyValue.put(UserRegisterType,UserRegisterCodePre);
        //用户上传文件路径前缀
        keyValue.put(UserUploadFileType,UserUploadFile);

        //商家登录验证码
        keyValue.put(MerchantLoginType,MerchantLoginCodePre);
        //商家注册验证码
        keyValue.put(MerchantRegisterType,MerchantRegisterCodePre);
        //商家上传文件路径前缀
        keyValue.put(MerchantUploadFileType,MerchantUploadFile);

    }

    public static String IfExistType(String type){
        if (keyValue.containsKey(type)){
            return keyValue.get(type);
        }else{
            log.info("不存在该类型");
        }
        return null;
    }
}
